<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>添加版本</title> {{>/WEB-INF/pages/common/meta_base.part.html}}
{{>/WEB-INF/pages/common/refs_base.part.html}}
{{>/WEB-INF/pages/common/refs_modal.part.html}}
{{>/WEB-INF/pages/common/refs_plupload.part.html}}
</head>
<body>
	<!-- <div class="container"> -->
	{{>/WEB-INF/pages/common/layout_header.part.html}}

	<div class="container">
		<ul class="breadcrumb">
			<li><a href="{{ctx}}">首页</a></li>
			<li><a href="{{ctx}}/mam/appMain/list-view">应用管理</a></li>
			<li><a href="{{ctx}}/mam/appMain/app-view/{{appvo.id}}">{{appvo.app_name}}</a></li>
			<li class="active">添加版本</li>
		</ul>
		<hr class="hr1" />
		<div class="hr-10"></div>
		<div class="container-fluid bor-tab bg-st-white pad-st-a10">
			<div id="tabs-604528" class="tabbable">
				<ul class="nav nav-tabs">
					<li class="active"><a data-toggle="tab" href="#panel-1"
						id="panel-1_active">基本信息</a></li>
					<li class=""><a data-toggle="tab" href="#"
						data-target="#panel-2" id="panel-2_active">关联模块</a></li>
					<li class=""><a data-toggle="tab" href="#"
						data-target="#panel-3" id="panel-3_active">生成应用</a></li>
				</ul>
				<div class="tab-content">
					<div id="panel-1" class="tab-pane active">
						<form id="dataForm" name="dataForm" method="post"
							enctype="multipart/form-data">
							<input type="hidden" name="applicationid" value="{{appvo.id}}"
								id="applicationid" /> <input type="hidden" name="platformid"
								value="{{platform}}" id="platformid" /> <input type="hidden"
								name="bundle" value="" id="bundle" /> <input type="hidden"
								name="status" value="1" id="status" />
							<div class="mar-st-a30">
								<div class="row pad-st-b10">
									<div class="col-sm-2 text-center">
										<img class="img-rounded" id="appIcon"
											style="width: 106px; height: 106px;" />
										<p class="text-muted">最新版本：{{lastVersion.version}}</p>
									</div>
									<div class="col-sm-9 text-left">
										<table>
											<tr>
												<td align="right" for="focusedInput">应用名称：</td>
												<td align="left"><input type="text" name="app_name"
													id="app_name" class="form-control" readonly="readonly"
													value="{{appvo.app_name}}" required /></td>
											</tr>
											<tr>
												<td align="right" for="focusedInput">应用标识：</td>
												<td align="left"><input type="text" name="identifier"
													id="identifier" class="form-control" readonly="readonly"
													value="{{appvo.identifier}}" required /></td>
											</tr>
											<tr>
												<td align="right" for="focusedInput">版本号：</td>
												<td align="left"><input type="text" name="version"
													onblur="checkVersion()" id="appVersion"
													class="form-control" value="" required /></td>
											</tr>
											<tr>
												<td align="right" for="focusedInput">构建号：</td>
												<td align="left"><input type="text" name="build"
													onblur="checkbuild()" id="buildInput" class="form-control"
													value="" required /></td>
											</tr>
											<tr>
												<td align="right">版本描述：</td>
												<td colspan="3" class="bd_sr" align="left"><textarea
														id="release_note" style="width: 284px; height: 79px;"
														name="release_note" cols="3" rows=""></textarea></td>
											</tr>
										</table>
									</div>
								</div>
								<div class="hr mar-st-tb10"></div>
								<div class="row">
									<div class="col-sm-4 text-left" id="{{platform}}showPlat">
										<div class="box-st-bg">
											<img src="{{ctx}}/static/bsl/images/{{platform}}002.gif"
												id="image_{{platform}}" style="height: 90px"
												class="img-rounded" />
											<div class="btn-info img-rounded" id="statusinfo">未激活</div>
											<br /> <a onclick="showMyCert('{{platform}}')"
												class="btn btn-primary btn-sm">上传安装包</a> <a
												class="btn btn-primary btn-sm"
												onclick="enterPackCert('{{platform}}')">生成安装包</a> <input
												type="hidden" name="seq" value="" id="packageSeq" />
										</div>
									</div>
								</div>
								<div class="container pad-st-b30 text-right">
									<a class="btn btn-primary btn-sm tls" type="button"
										onclick="saveForm()" id="btn-primarySubmit">保存</a> <a
										class="btn btn-common btn-sm tls" class="btn"
										href="{{ctx}}/mam/appMain/app-view/{{appvo.id}}">返回</a>
								</div>
							</div>
						</form>
					</div>
					<div id="panel-2" class="tab-pane">
						<div class="container-fluid text-center">
							<img src="{{ctx}}/static/bsl/images/loadImage.gif" border="0"
								style="width: 50px; height: 50px" alt="loading..." id="load" />
						</div>
					</div>

					<div id="panel-3" class="tab-pane"></div>
				</div>
			</div>
		</div>
	</div>
	{{>/WEB-INF/pages/app/widget-modal.html}}
	<!-- 选择证书 -->
	{{>/WEB-INF/pages/app/pack-modal.html}}
	<!-- footer 脚本 -->
	{{>/WEB-INF/pages/common/layout_footer.part.html}}
</body>
</html>

<script type="text/javascript">
	// 提示框
	var popover = function(inputId, errorMsg) {
		var el = $('#' + inputId);
		var controls = el.parent().find('.a');
		var len = controls.length;
		if (errorMsg) {
			var cls = (el.attr('btvd-class') == undefined) ? null : el.attr(
					'btvd-class').split(' ');
			if (len <= 0) {
				el
						.after('<input class="a"  style=" visibility:hidden;" data-placement="bottom"  data-content="'+errorMsg+ '"  data-toggle="popover"/>');
			}
			controls.attr("data-content", errorMsg);
			controls.show();
			el.next().popover("show");
			var pop = el.parent().find(".popover"), pos = pop.offset();
			pos.top = pos.top - el.next().height();
			pop.offset(pos);
			if (cls)
				pop.addClass("btvdclass");

		} else {
			controls.popover("hide");
			controls.hide();
		}
	}

	$(document).ready(
			function() {
				var icon_id = "{{appvo.icon}}";
				if (icon_id != "") {
					jQuery("#appIcon").attr("src",
							"{{ctx}}/mam/attachment/download/{{appvo.icon}}");
				} else {
					jQuery("#appIcon").attr("src",
							"{{ctx}}/static/bsl/images/defind.png");

				}

				$('#panel-2_active').click(function(e) {
					e.preventDefault();
					var version = $('#appVersion').val();
					var appId = $('#applicationid').val();
					var platform = $('#platformid').val();
					if (version === "") {
						popover("appVersion", "请填写版本号");
						return false;
					}
					reloadPanel(platform, version, appId)
					$(this).tab('show')
				});

				$('#panel-3_active').click(
						function(e) {
							e.preventDefault();
							$('#panel-3').empty();
							var appId = $('#applicationid').val();
							var version = $('#appVersion').val();
							var platform = $('#platformid').val();
							if (version === "") {
								popover("appVersion", "请填写版本号");
								return false;
							}
							var loadurl = "{{ctx}}/mam/logos/" + platform + "/"
									+ version + "/" + appId;
							$.get(loadurl, function(data) {
								$('#panel-3').html(data)
							});
							$(this).tab('show')
						});

			});

	//定时检查打包状态，打包时触发该函数
	var interval;
	function reloadInterval() {
		interval = window.setInterval(function() {
			findBySeq();
		}, 10000);
	}

	//定时器刷新打包状态
	function findBySeq() {
		var packageSeq = $('#packageSeq').val();
		var url = "{{ctx}}/mam/pkg/findBySeq" + "?task_id=" + packageSeq
				+ "&t=" + new Date();
		$.ajax({
			async : false,
			type : "GET",
			url : url,
			dataType : 'json',
			success : function(data, textStatus) {
				if (data.result === "success") {
					if (data.status != "2") {
						var atag = $('#packageSeq').parent().find('a');
						$(atag).each(function(index) {
							$(this).removeAttr("disabled");
						});
						if (data.status === "3") {
							$("#statusinfo").text("已激活");
							$('#status').val("3");
							$('#bundle').val(data.bundle);
						} else {
							$("#statusinfo").text("打包失败");
							$('#status').val("0");
						}
						$('#btn-primarySubmit').removeAttr("disabled");
						clearInterval(interval);//移除定时器
					}
				}
			}
		});
	}

	function checkVersion() {
		var version = $('#appVersion').val();
		var tel = /^\d{1,2}\.\d{1,2}\.\d{1,2}$/;
		var flag = tel.test(version);
		if (!flag) {
			popover("appVersion", "版本号格式不正确，请重新填写。例如：1.0.0");
			$('#appVersion').val("")
		} else {
			var oldVersion = "{{lastVersion.version}}";
			var oldReplace = oldVersion.replace(/\./g, "");
			var newReplace = version.replace(/\./g, "");
			if (parseInt(oldReplace) >= parseInt(newReplace)) {
				popover("appVersion", "版本号必须大于最新版本" + oldVersion);
				$('#appVersion').val("")
			} else {
				popover("appVersion");
			}
		}
	}

	function checkbuild() {
		var build = $('#buildInput').val();
		var tel = /^[0-9]\d*$/;
		var flag = tel.test(build);
		if (!flag) {
			popover("buildInput", "此输入项必须为数字！");
			$('#buildInput').val("")
		} else {
			var oldBuild = "{{lastVersion.build}}";
			var build = $('#buildInput').val();
			if (parseInt(oldBuild) >= parseInt(build)) {
				popover("buildInput", "构建号必须大于" + oldBuild);
				$('#buildInput').val("");
			} else {
				popover("buildInput");
			}
		}
	}

	//重新加载模块
	function reloadPanel(platform, version, appId) {
		var loadurl = "{{ctx}}/mam/appwidgets/" + platform + "/" + version
				+ "/relations/" + appId + "?t=" + new Date();
		$.get(loadurl, function(data) {
			$('#panel-2').html(data)
		});
	}

	function enterList() {
		window.location.href = "{{ctx}}/mam/appMain/list-view";
	}

	function enterPackCert(platformid) {
		var appId = $('#applicationid').val();
		var build = $('#buildInput').val();
		var version = $('#appVersion').val();
		if (version == "") {
			popover("appVersion", "请填写版本号");
			return;
		} else if (build == "") {
			popover("buildInput", "请填写构建号！");
			return;
		}
		popover("appVersion");
		popover("buildInput");

		$('#panel-3_active').click();
		return;
	}

	//保存
	function saveForm() {
		var version = $('#appVersion').val();
		var build = $('#buildInput').val();
		if (version == "") {
			popover("appVersion", "请填写版本号");
			return;
		} else if (build == "") {
			popover("buildInput", "请填写构建号！");
			return;
		}
		popover("appVersion");
		popover("buildInput");
		var data = $('#dataForm').serialize();
		$.ajax({
			async : false,
			type : "POST",
			url : "{{ctx}}/mam/appversions/{{platform}}",
			data : data,
			dataType : 'json',
			success : function(data, textStatus) {
				if (data.result == "success") {
					if (data.statusCode === "1") {
						var url = "{{ctx}}/mam/appMain/app-view/{{appvo.id}}";
						bootbox.alert("保存成功", function() {
							window.location.href = url;
						});
					} else {
						bootbox.alert("版本号已经存在，请修改");
					}
				} else {
					bootbox.alert("保存失败，请稍后再试！");
				}
			},
			error : function(data, status, e) {
				bootbox.alert("保存失败，请稍后再试！");
			}
		});
	}
</script>